package com.gadgets.framework.common.util;

/**
 * @author UncleXiu
 * @version V1.0.1
 * @data 2021-01-28
 * @moduel gadgets
 *
 */
public class TypeCompareUtil {

    /**
     * 判断两个对象是否是某个类型的子类型对象
     * @param a             目标对象
     * @param targetClazz   目标类
     * @return              是否一致
     */
    @SuppressWarnings("rawtypes")
    public static boolean instanceOf(Object a, Class targetClazz){
        Class clazzA = a.getClass();
        if(equalClass(clazzA,targetClazz)){
            return true;
        }else{
            boolean test = false;
            while (clazzA!=Object.class || !(test = equalClass(clazzA.getSuperclass(),targetClazz))){
                clazzA = clazzA.getSuperclass();
            }
            return test;
        }
    }

    /**
     * 判断两个类是否为同一个类
     * @param a 类A
     * @param b 类B
     * @return  比较结果
     */
    @SuppressWarnings("rawtypes")
    public static boolean equalClass(Class a, Class b){
        return a.getName().equals(b.getName());
    }

}
